{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/125_using_example_cde_.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Using Earth engine examples**\n",
    "\n",
    "\n",
    "- ee datasets seem to come from https://code.earthengine.google.com/scripts/load\n",
    "- When logged into gee, this link returns a f.json. It contains all code examples from the docs, including the datasets.\n",
    "- community datasets can be accessed using git from https://earthengine.googlesource.com\n",
    "- which in many cases can be translated into python code\n",
    "- this should make it feasible to use the code examples directly into geemap\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -U geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import json"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Manually download f.json \n",
    "https://code.earthengine.google.com/scripts/load"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## Explore json contents\n",
    "\n",
    "Change file download location. Also added f.json to repo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://github.com/gee-community/geemap/blob/master/geemap/data/gee_f.json\"\n",
    "geemap.download_file(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"gee_f.json\", encoding=\"utf-8\") as f:\n",
    "    functions = json.load(f)\n",
    "\n",
    "functions.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "[(x[\"name\"], len(x[\"contents\"])) for x in functions[\"examples\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "## Details"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "details = [\n",
    "    (dataset[\"name\"], dataset[\"code\"])\n",
    "    for x in functions[\"examples\"]\n",
    "    for dataset in x[\"contents\"]\n",
    "    if x[\"name\"] == \"Datasets\"\n",
    "]\n",
    "details[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "## Convert js to py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_py(js):\n",
    "    try:\n",
    "        return geemap.js_snippet_to_py(\n",
    "            js, add_new_cell=False, import_ee=False, import_geemap=False, show_map=False\n",
    "        )\n",
    "    except:\n",
    "        return None\n",
    "\n",
    "\n",
    "results_convert = [get_py(js) for _, js in details]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(details), len(results_convert)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "Except for 2 cases, converts ran without error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "len([x for x in results_convert if x])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "## Verify availability"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "from geemap import datasets\n",
    "\n",
    "len(datasets.get_ee_stac_list())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "known_datasets = set(name.replace(\"/\", \"_\") for name in datasets.get_ee_stac_list())\n",
    "known_code = set(name for name, _ in details)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18",
   "metadata": {},
   "source": [
    "## Only some have exact matches"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(known_code & known_datasets)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20",
   "metadata": {},
   "source": [
    "Sometimes there is a 2 example available for a dataset, mostly FeatureViews."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "excess = known_code - known_datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(excess)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23",
   "metadata": {},
   "outputs": [],
   "source": [
    "excess_no_featureview = set(x.replace(\"_FeatureView\", \"\") for x in known_code)\n",
    "len(excess_no_featureview - known_datasets)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24",
   "metadata": {},
   "source": [
    "Leftovers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "(excess_no_featureview - known_datasets)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26",
   "metadata": {},
   "source": [
    "## Datasets without examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "missing_code = known_datasets - known_code\n",
    "len(missing_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28",
   "metadata": {},
   "source": [
    "These are also missing in gee itself (logically as these load from f.json). \n",
    "\n",
    "- https://code.earthengine.google.com/?scriptPath=Examples%3ADatasets%2FLANDSAT_LC8\n",
    "\n",
    "Deprecated stuff afaik, s.a. Landsat LT1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29",
   "metadata": {},
   "outputs": [],
   "source": [
    "missing_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30",
   "metadata": {},
   "outputs": [],
   "source": [
    "from geemap import common\n",
    "\n",
    "[x[\"title\"] for x in common.search_ee_data(\"LANDSAT L1T\")][:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31",
   "metadata": {},
   "outputs": [],
   "source": [
    "[(x[\"uid\"], x[\"title\"]) for x in common.search_ee_data(\"MODIS MYD\")][:5]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
